package org.bangumibuddy.service.Impl;

import lombok.extern.slf4j.Slf4j;
import org.bangumibuddy.entity.*;
import org.bangumibuddy.pojo.anime.Anime;
import org.bangumibuddy.pojo.anime.AnimeSummary;
import org.bangumibuddy.pojo.TorrentFile;
import org.bangumibuddy.exception.DataBaseQueryException;
import org.bangumibuddy.mapper.*;
import org.bangumibuddy.service.AnimeService;
import org.bangumibuddy.service.MovieScrapingService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class AnimeServiceImpl implements AnimeService {

    @Autowired
    AnimeMapper animeMapper;

    @Autowired
    AnimeSummaryMapper animeSummaryMapper;

    @Autowired
    RssMapper rssMapper;

    @Autowired
    TorrentsMapper torrentsMapper;

    @Override
    public List<AnimeEntity> getAnimaList() {
        return animeMapper.getAnimeList();
    }

    @Autowired
    MovieScrapingService movieScrapingService;

    @Override
    public AnimeSummary getAnimaSummary(Long id) {
        AnimeEntity animaDao = animeMapper.getAnime(id);
        if (animaDao == null)
            throw new DataBaseQueryException();

        AnimeSummaryEntity animeSummaryEntity = animeSummaryMapper.getAnimeSummaryByAnimaId(id);
        AnimeSummary summary = new AnimeSummary();
        BeanUtils.copyProperties(animaDao, summary);
        BeanUtils.copyProperties(animeSummaryEntity, summary);

        return summary;
    }

    @Override
    @Transactional
    public boolean addAnime(Anime anime) {
        // 1. 添加Anima

        AnimeEntity animeEntity = new AnimeEntity();
        BeanUtils.copyProperties(anime, animeEntity);
        animeEntity.setPoster(anime.getSummary().getPoster());
        animeMapper.addAnime(animeEntity);

        // 2. 添加Rss
        RssEntity rss = new RssEntity();
        rss.setAnimeId(animeEntity.getId());
        rss.setUrl(anime.getRssUrl());
        rss.setName(anime.getRssName());
        rss.setIncludePatterns(anime.getIncludePatterns());
        rss.setExcludePatterns(anime.getExcludePatterns());
        rssMapper.addRss(rss);

        // 3. 添加Torrent
        List<TorrentEntity> torrentEntities = anime.getTorrentFiles().stream().map(torrent -> {
            TorrentEntity torrentEntity = new TorrentEntity();
            BeanUtils.copyProperties(torrent, torrentEntity);
            torrentEntity.setAnimeId(animeEntity.getId());
            torrentEntity.setRssId(rss.getId());
            return torrentEntity;
        }).toList();
        torrentsMapper.addTorrents(torrentEntities);

        // 4. 添加Summary
        AnimeSummary animeSummary = movieScrapingService.getAnimeSummary(anime.getName(), anime.getSeasonNumber(), "zh");
        AnimeSummaryEntity animeSummaryEntity = new AnimeSummaryEntity();
        BeanUtils.copyProperties(animeSummary == null ? anime.getSummary() : animeSummary, animeSummaryEntity);
        animeSummaryEntity.setAnimeId(animeEntity.getId());
        animeSummaryMapper.addAnimeSummary(animeSummaryEntity);

        return true;
    }
}
